<?php

namespace app\index\controller;

use think\Controller;
use think\Facade\Request;
use app\index\model\Vehicle as Model;
use app\index\model\Driver;
use app\index\validate\Vehicle as Validate;
use app\index\model\Warehouse;
class Vehicle extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if(Request::isPost()){
            $where='';
            $name= input('post.name');
            $warehouse = Request::post('warehouse');
            if($name!=''){
                $where[] = ['name', 'like', "%{$name}%"];
            }

            if($warehouse)
            {
                $where['warehouse'] = $warehouse;
            }
            $order = Model::where($where)->paginate(10);
            $total= Model::where($where)->count();
            $data = $order->items();
            returnAjax('200',[
                'data'=>$data,
                'total'=>$total,
                'page'=>input('get.page',1)
            ]);
        }
        $wareHouse = Warehouse::all();
        return $this->fetch('',['wareHouse'=>$wareHouse]);
    }

    /**
     * 获取车辆列表（包括分配状态）
     * @return mixed
     */
    public function assignVehicleLists()
    {
        if(Request::isPost()){
            $where='';
            $name= input('post.name');
            if($name!=''){
                $where[] = ['name', 'like', "%{$name}%"];
            }
            $order = Model::where($where)->paginate(10);
            $total= Model::where($where)->count();
            $data = $order->items();
            foreach ( $data as $key=> $value )   //查询车辆是否被分配
            {
                 $isHas = Driver::where(['vehicle_id'=>$value['vehicle_id']])->count();
                 $data[$key]['isAssign'] = $isHas ? true : false;
            }
            returnAjax('200',[
                'data'=>$data,
                'total'=>$total,
                'page'=>input('get.page',1)
            ]);
        }
        return $this->fetch();
    }
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $wareHouse = Warehouse::all();
        return $this->fetch('',['wareHouse'=>$wareHouse]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        if(Request::isPost())
        {
            $post = Request::post();
            $validate = new Validate();
            if (!$validate->check($post)) {
                returnAjax('201',$validate->getError());
            }
            if( Model::isRepeatVehicleId($post['vehicle_id']) )
            {
                returnAjax('201','Vehicle Id已存在');
            }
            $res = Model::create($post);
            if( $res )
            {
                returnAjax('200','添加成功');
            }
            returnAjax('201','添加失败');

        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        $id = Request::get('id');
        $data = Model::where(['id'=>$id])->find();
        $wareHouse = Warehouse::all();
       return $this->fetch('',['data'=>$data,'wareHouse'=>$wareHouse]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if(Request::isPost())
        {
            $post = Request::post();
            $result = $this->validate($post,'app\index\validate\Vehicle.edit');
            if (!$result) {
                returnAjax('201',$result);
            }
            $res = Model::where(['id'=>$post['id']])->update($post);
            if( $res )
            {
                returnAjax('200','修改成功');
            }
            returnAjax('201','修改失败');

        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
